package com.lanyou.cook.camel.util;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import org.apache.log4j.Logger;

public class CommUtil {
	private static Logger logger = Logger.getLogger(CommUtil.class);
	
	public static boolean IS_WIN = false;
	public static boolean IS_LINUX = false;
	public static String EXECUTE_TASK_NODE = "0.0.0.0";
	static{
		String os = System.getProperty("os.name"); 
		if(os.toLowerCase().startsWith("win")){
			IS_WIN = true;
			EXECUTE_TASK_NODE = getWinIp();
		}else{
			IS_LINUX = true;
			EXECUTE_TASK_NODE = getLinuxIP();
		}
		
		 
	}
	public static String getWinIp(){
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			return addr.getHostAddress().toString();//获得本机IP
		} catch (UnknownHostException e) {
			logger.error(ExceptionToStrFactory.getErrorInfoFromException(e));
			return "0.0.0.0";
		}
	}
	public static String getLinuxIP() {
        String ip = "";
        try {
            Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
            while (e1.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) e1.nextElement();
                if (!ni.getName().contains("eth")) {
                    continue;
                } else {
                    Enumeration<?> e2 = ni.getInetAddresses();
                    while (e2.hasMoreElements()) {
                        InetAddress ia = (InetAddress) e2.nextElement();
                        if (ia instanceof Inet6Address)
                            continue;
                        ip = ia.getHostAddress();
                    }
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return ip;
    }


}
